package com.shsoft.task;

import com.shsoft.MonitorApplication;
import com.shsoft.entity.HardWareInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@EnableScheduling
public class MonitorScheduled {

    @Autowired
    private ThreadPoolTaskExecutor myAsyncThreadPool;

    @Scheduled(initialDelay = 2 * 1000L, fixedDelayString = "1500")
    private void checkOnline() {
        if(MonitorApplication.mainController != null){
            MonitorApplication.mainController.showInfos();
        }
    }

    @Scheduled(initialDelay = 10 * 1000L, fixedDelay = 60 * 60 * 1000)
    private void queryWeather() {
        while (MonitorApplication.mainController == null){ // 等待界面初始化完毕
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        if(!StringUtils.isEmpty(MonitorApplication.settingConfig.getKey()) && !StringUtils.isEmpty(MonitorApplication.settingConfig.getCityID())){
            myAsyncThreadPool.execute(new WeatherTask(MonitorApplication.settingConfig.getKey(), MonitorApplication.settingConfig.getCityID(), MonitorApplication.mainController, myAsyncThreadPool));
            HardWareInfo.cityName = MonitorApplication.settingConfig.getCityName();
        }
    }

}
